package com.packet.cheng.service.impl;
import com.packet.cheng.dto.SysLogDTO;
import com.packet.cheng.entity.SysLog;
import com.packet.cheng.mapper.SysLogMapper;
import com.packet.cheng.service.SysLogService;
import com.packet.cheng.util.R;
import com.packet.cheng.util.StringUtil;
import com.packet.cheng.vo.SysLogVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author 张在成
 */
@Service
public class SysLogServiceImpl implements SysLogService {

    @Resource
    private SysLogMapper sysLogMapper;


    @Override
    public R<Integer> insertSysLog(SysLogDTO sysLogDTO) throws Exception {
        if (StringUtil.isBlank(sysLogDTO.getUsername())){
            return R.data(405,"用户名不能为空");
        }
        if (StringUtil.isBlank(sysLogDTO.getIp())) {
            return R.data(405, "ip不能为空");
        }
        SysLog sysLog = new SysLog();
        BeanUtils.copyProperties(sysLogDTO,sysLog);
        int insertSysLog = sysLogMapper.insertSysLog(sysLog);
        if (insertSysLog == 0){
            return R.data(405,"系统日志记录失败");
        }
        return R.data(200,"success",1);
    }

    @Override
    public R<SysLogVO> selectSysLogBySysLogId(Integer sysLogId) throws Exception {
        if (StringUtil.isBlank(String.valueOf(sysLogId))){
           return R.data(405,"系统日志id不能为空");
        }
        SysLogVO sysLogVO = sysLogMapper.selectSysLogBySysLogId(sysLogId);
        if (Objects.isNull(sysLogVO)){
            return  R.data(405,"系统日志不存在");
        }
        return R.data(200,"success",sysLogVO);
    }

    @Override
    public R<List<SysLogVO>> selectSysLog(SysLogDTO sysLogDTO) throws Exception {
        SysLog sysLog = new SysLog();
        BeanUtils.copyProperties(sysLogDTO,sysLog);
        List<SysLogVO> sysLogVOS = sysLogMapper.selectSysLogList(sysLog);
        if (Objects.isNull(sysLogVOS)){
            return  R.data(405,"系统日志不存在");
        }
        return R.data(200,"success",sysLogVOS);
    }
}
